from lxml import etree

# xpath解析
# 1、本地文件 etree.parse()
# 2、服务器响应的数据 response.read().decode("utf-8") etree.HTML()

tree = etree.parse("17_解析_xpath的基本使用.html")
print(tree)

# tree.xpath(xpath路径)
# 查找ul下面的li
# li_list = tree.xpath("//body/ul/li")
# print(len(li_list))

# 查找所有id的属性的li标签
# li_list = tree.xpath("//ul/li[@id]/text()")

# 找到id为l1的标签 注意引号的问题
# li_list = tree.xpath("//ul/li[@id='l1']/text()")

# 查找到id为l1的标签的class属性值
# li_list = tree.xpath("//ul/li[@id='l1']/@class")

# 查找id中包含l的li标签
# li_list = tree.xpath("//ul/li[contains(@id, 'l')]/text()")

# 查询id的值以l开头的li标签
# li_list = tree.xpath("//ul/li[starts-with(@id, 'l')]/text()")

# 查询 id为l1和class为c1的
# li_list = tree.xpath("//ul/li[@id='l1' and @class='c1']/text()")

# 查询
li_list = tree.xpath("//ul/li[@id='l1']/text() | //ul/li[@id='l2']/text()")
print(li_list)
print(len(li_list))